package com.mars.module.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mars.common.response.PageInfo;
import com.mars.framework.context.ContextUserInfoThreadHolder;
import com.mars.module.admin.entity.SysLeaveMessage;
import com.mars.module.admin.service.ISysDictDataService;
import com.mars.module.system.entity.SysUser;
import com.mars.module.system.mapper.SysUserMapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import com.mars.module.admin.request.SysNoticeRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import com.mars.module.admin.mapper.SysNoticeMapper;
import org.springframework.beans.BeanUtils;
import com.mars.module.admin.entity.SysNotice;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mars.module.admin.service.ISysNoticeService;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 系统公告业务层处理
 *
 * @author mars
 * @date 2024-03-14
 */
@Slf4j
@Service
@AllArgsConstructor
public class SysNoticeServiceImpl implements ISysNoticeService {

    private final SysNoticeMapper baseMapper;
    private final SysUserMapper userMapper;
    private final ISysDictDataService dataService;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public SysNotice add(SysNoticeRequest request) {
        SysNotice entity = SysNotice.builder().build();
        BeanUtils.copyProperties(request, entity);
        entity.setStatus("0");
        Long userId = ContextUserInfoThreadHolder.get().getId();
        entity.setCreateById(userId);
        baseMapper.insert(entity);
        return entity;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(Long id) {
        return baseMapper.deleteById(id) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteBatch(List<Long> ids) {
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(SysNoticeRequest request) {
        SysNotice entity = SysNotice.builder().build();
        BeanUtils.copyProperties(request, entity);
        return baseMapper.updateById(entity) > 0;
    }

    @Override
    public SysNotice getById(Long id) {
        return baseMapper.selectById(id);
    }

    @Override
    public PageInfo<SysNotice> pageList(SysNoticeRequest request) {
        Page<SysNotice> page = new Page<>(request.getPageNo(), request.getPageSize());
        LambdaQueryWrapper<SysNotice> query = this.buildWrapper(request);
        IPage<SysNotice> pageRecord = baseMapper.selectPage(page, query);
        List<SysNotice> records = pageRecord.getRecords();
        List<SysNotice> recordsNew = new ArrayList<>();
        for (SysNotice e : records) {
            if (StringUtils.isNotBlank(e.getStatus())) {
                e.setStatusName(dataService.getDictDataById("notice_status", e.getStatus()));
            }
            SysUser sysUser = userMapper.selectById(e.getCreateById());
            e.setCreateByName(sysUser.getUserName());
            recordsNew.add(e);
        }
        pageRecord.setRecords(recordsNew);
        return PageInfo.build(pageRecord);
    }


    @Override
    public List<SysNotice> list(SysNoticeRequest request) {
        LambdaQueryWrapper<SysNotice> query = this.buildWrapper(request);
        return baseMapper.selectList(query);
    }

    private LambdaQueryWrapper<SysNotice> buildWrapper(SysNoticeRequest param) {
        LambdaQueryWrapper<SysNotice> query = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(param.getTitle())) {
            query.like(SysNotice::getTitle, param.getTitle());
        }
        if (StringUtils.isNotBlank(param.getContent())) {
            query.like(SysNotice::getContent, param.getContent());
        }
        if (StringUtils.isNotBlank(param.getStatus())) {
            query.like(SysNotice::getStatus, param.getStatus());
        }
        if (StringUtils.isNotBlank(param.getPicture())) {
            query.like(SysNotice::getPicture, param.getPicture());
        }
        return query;
    }

}
